package com.hxrfid.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.system.domain.SysTempMenu;
import com.hxrfid.system.domain.bo.SysTempMenuBo;
import com.hxrfid.system.domain.vo.SysTempMenuVo;
import com.hxrfid.system.mapper.SysTempMenuMapper;
import com.hxrfid.system.service.ISysTempMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 系统模板菜单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-08
 */
@RequiredArgsConstructor
@Service
public class SysTempMenuServiceImpl implements ISysTempMenuService {

    private final SysTempMenuMapper baseMapper;

    /**
     * 查询系统模板菜单
     */
    @Override
    public SysTempMenuVo queryById(Long tempId){
        return baseMapper.selectVoById(tempId);
    }

    /**
     * 查询系统模板菜单列表
     */
    @Override
    public TableDataInfo<SysTempMenuVo> queryPageList(SysTempMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysTempMenu> lqw = buildQueryWrapper(bo);
        Page<SysTempMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统模板菜单列表
     */
    @Override
    public List<SysTempMenuVo> queryList(SysTempMenuBo bo) {
        LambdaQueryWrapper<SysTempMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysTempMenu> buildQueryWrapper(SysTempMenuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysTempMenu> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTempId() != null, SysTempMenu::getTempId, bo.getTempId());
        lqw.eq(bo.getMenuId() != null, SysTempMenu::getMenuId, bo.getMenuId());
        return lqw;
    }

    /**
     * 新增系统模板菜单
     */
    @Override
    public Boolean insertByBo(SysTempMenuBo bo) {
        SysTempMenu add = BeanUtil.toBean(bo, SysTempMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTempId(add.getTempId());
        }
        return flag;
    }

    /**
     * 修改系统模板菜单
     */
    @Override
    public Boolean updateByBo(SysTempMenuBo bo) {
        SysTempMenu update = BeanUtil.toBean(bo, SysTempMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysTempMenu entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统模板菜单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
